<template>
  <div class="main add-training-plan">
    <van-field
      label="姓名"
      class="textarea"
      placeholder="请输入"
      required
      v-model="data.ChnName"
      name="area"
    >
    </van-field>
    <van-field
      label="工号"
      placeholder="请输入"
      autosize
      class="textarea"
      v-model="data.WorkCode"
    ></van-field>

    <van-field
      placeholder="请输入"
      label="部门"
      class="textarea"
      v-model="data.OrgCode"
    >
      <!-- <template #input>
				<div v-if="data.Details2 && data.Details2.length" style="color: #999">
					已选择{{ data.Details2.length }}个人员
				</div>
				<span v-else style="color: #999"> 请选择0个人员</span>
			</template> -->
    </van-field>

    <van-field
      autosize
      placeholder="请输入"
      label="岗位"
      class="textarea"
      v-model="data.PostCode"
    >
      <!-- <template #input>
				<div v-if="data.Details2 && data.Details2.length" style="color: #999">
					已选择{{ data.Details2.length }}个人员
				</div>
				<span v-else style="color: #999"> 请选择0个人员</span>
			</template> -->
    </van-field>

    <!-- <van-field
      autosize
      placeholder="请输入"
      label="兼职岗位"
      class="textarea"
      v-model="data.ChildPostCodes"
    >
    </van-field> -->

    <van-field
      label="手机号"
      placeholder="请输入"
      autosize
      class="textarea"
      v-model="data.Mobile"
    ></van-field>
    <van-field
      label="邮箱"
      placeholder="请输入"
      autosize
      class="textarea"
      v-model="data.Email"
    ></van-field>

    <div class="bottom_submit">
      <van-button type="primary" @click="_submit()"> 保存 </van-button>
    </div>
  </div>
</template>

<script>
import store from "@/store/index";
import { Save,getUserDetail } from "@/api/userUser";
export default {
  data() {
    return {
      data: {
        UserName:"",
      },
    };
  },
  beforeRouteLeave(to, from, next) {
    console.log(to, "to");
    if (to.path == "/Train" || to.path == "/Train/TrainingPlanList") {
      store.commit("updataTrainingPlanData", "");
      store.commit("updataSetPlanExamData", "");
    }
    next(); //继续下一步
  },

  created() {
    let title;
    let userName = this.$route.query.userName;
    if (userName) {
      title = "员工编辑";
      this.data.UserName = userName;
    } else {
      title = "员工新增";
    }
    store.commit("updateTitle", title);
    if(this.data.UserName)
    {
      this.getDetail()
    }
  },
  methods: {
    // 保存
    _submit() {
      if (!this.data.ChnName) {
        return this.$toast("请输入培训计划名称");
      }
      if (this.$route.query.id > 0) {
      }
      const formData = {
        UserName: this.data.UserName,
        ID: this.data.ID,

        CompanyCode: this.data.CompanyCode,
        ChnName: this.data.ChnName,
        WorkCode: this.data.WorkCode,
        OrgCode: this.data.OrgCode,
        PostCode: this.data.PostCode,
        //ChildPostCodes: this.data.ChildPostCodes,
		    Mobile: this.data.Mobile,
        Email: this.data.Email
      };

      Save(formData).then((res) => {
        this.$toast("保存成功");
        setTimeout(() => {
          // this.$router.push('/Train')
          this.$router.go(-1);
        }, 500);
      });
    },
    getDetail() {
      let params = { userName: this.data.UserName };
      if (this.data.UserName) {
        getUserDetail(params).then((res) => {
          this.data = res.data;
        });
      }
    },
  },
};
</script>
<style lang="less">
.add-training-plan {
  .textarea {
    .van-field__control {
      background-color: #f8f8f8 !important;
    }
  }
}
</style>
<style lang="less" scoped>
.col-detail {
  color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .col-del {
    display: flex;
    align-items: center;
    img {
      width: 15px;
      height: 15px;
      margin-right: 5px;
    }
  }
}
.valueclass {
  color: rgb(0, 195, 224);
}
.examine-text {
  font-size: 10px;
  color: #999;
}
.title {
  display: inline-block;
  position: relative;
  color: #646566;
}
.title::before {
  position: absolute;
  left: -0.21333rem;
  color: #c03131;
  font-size: 0.37333rem;
  z-index: 999;
  content: "*";
}
.bottom_submit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 15px;
  box-sizing: border-box;
  .van-button {
    width: 100%;
    height: 35px;
  }
}
</style>
